package adv46.chat;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

/**
 * @author oldliu
 * @since 1.0
 */
public class ChatServer {
    public static void main(String[] args) {
        try {
            ServerSocket st=new ServerSocket(8888);
            while (true){
                Socket s = st.accept();
                InputStream in = s.getInputStream();
                OutputStream out = s.getOutputStream();
                System.out.println("可用字节："+in.available());
                BufferedReader br=new BufferedReader(new InputStreamReader(in));
                PrintWriter pw=new PrintWriter(out,true);
                //聊天采用，客户端问，服务器回答的方式
                while (true){
                    String ask=br.readLine();
                    if(ask==null||ask.length()==0)
                        continue;
                    System.out.println("客户端："+s.getInetAddress()+"问："+ask);
                    pw.println("你的问题"+ask+"，朕知道了，"+new Date());
                    if(ask.equals("quit"))
                        break;
                }
                s.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
